package com.itheima.health.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.itheima.health.constant.MessageConstant;
import com.itheima.health.entity.PageResult;
import com.itheima.health.entity.QueryPageBean;
import com.itheima.health.entity.Result;
import com.itheima.health.exception.HealthException;
import com.itheima.health.pojo.CheckItem;
import com.itheima.health.service.CheckItemService;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * @Author tang.qingbo
 * @Date 2020/7/26 17:47
 * @Version 1.0
 */
@RestController
@RequestMapping("/checkitem")
public class CheckItemController {
    /**
     * 依赖CheckItemService,使用@Reference引入
     */
    @Reference
    private CheckItemService checkItemService;

    /**
     * 页面加载完成,查询所有检查项(不分页)
     */
    @RequestMapping("/findAll")
    public Result findAll() {
        List<CheckItem> checkItems = null;
        try {
            checkItems = checkItemService.findAll();
        } catch (Exception e) {
            //出现异常,false
            return new Result(false, MessageConstant.QUERY_CHECKITEM_FAIL);
        }
        return new Result(true, MessageConstant.QUERY_CHECKITEM_SUCCESS, checkItems);
    }

    /**
     * 添加检查项
     */
    @RequestMapping("/add")
    public Result add(@RequestBody CheckItem checkItem) {
        try {
            checkItemService.add(checkItem);
        } catch (Exception e) {
            //返回false
            return new Result(false, MessageConstant.ADD_CHECKITEM_FAIL);
        }
        return new Result(true, MessageConstant.ADD_CHECKITEM_SUCCESS);
    }

    /**
     * 分页查询检查项
     */
    @RequestMapping("/findPage")
    public Result findPage(@RequestBody QueryPageBean queryPageBean) {
        PageResult<CheckItem> pageResult = null;
        try {
            pageResult = checkItemService.findPage(queryPageBean);
        } catch (Exception e) {
            //抛异常就返回false
            return new Result(false, MessageConstant.QUERY_CHECKITEM_FAIL);
        }
        //结果封装到result返回
        return new Result(true, MessageConstant.QUERY_CHECKITEM_SUCCESS, pageResult);
    }

    /**
     * 根据id删除
     */
    @RequestMapping("/deleteById")
    public Result deleteById(Integer id) {
        checkItemService.deleteById(id);
        //删除成功
        return new Result(true, MessageConstant.DELETE_CHECKITEM_SUCCESS);
    }

    /**
     * 根据id查询检查项
     */
    @RequestMapping("/findById")
    public Result findById(Integer id) {
        CheckItem checkItem = null;
        try {
            checkItem = checkItemService.findById(id);
        } catch (Exception e) {
            return new Result(false, MessageConstant.QUERY_CHECKITEM_FAIL);
        }
        //返回结果
        return new Result(true, MessageConstant.QUERY_CHECKITEM_SUCCESS, checkItem);
    }

    /**
     * 修改检查项
     */
    @RequestMapping("/update")
    public Result update(@RequestBody CheckItem checkItem) {
        try {
            checkItemService.update(checkItem);
        } catch (Exception e) {
            return new Result(false, MessageConstant.EDIT_CHECKITEM_FAIL);
        }
        //返回数据
        return new Result(true, MessageConstant.EDIT_CHECKITEM_SUCCESS);
    }
}
